﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.ParameterDTO;
using uMES.LeanManufacturing.ReportBusiness;
using uMES.LeanManufacturing.DBUtility;

public partial class Report_BwSpecWorkHoursChangeReportForm : PageBase
{
    int m_PageSize = 50;
    uMESReportBusiness reportBal = new uMESReportBusiness();
    protected void Page_Load(object sender, EventArgs e)
    {
        uMESMasterPage master = this.Master as uMESMasterPage;
        master.strNavigation = "当前位置：临时工艺工序工时修改记录";
        master.strTitle = "临时工艺工序工时修改记录";
        master.ChangeFrame(true);
        upageTurning.PageIndexChanged += new pageTurning.PageIndexChangedEventHandler(() => { SearchData(upageTurning.CurrentPageIndex.ToString()); });
        WebPanel = WebAsyncRefreshPanel1;
        ClearMessage();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        try { gdResult.Rows.Clear(); SearchData("1"); } catch (Exception ex) { DisplayMessage(ex.Message, false); }
    }
    protected void btnReSet_Click(object sender, EventArgs e)
    {
        txtGYY.Text = "";txtProductName.Text = "";txtStartDate.Value = "";txtEndDate.Value="" ; txtDEY.Text = ""; txtProcessNo.Text = ""; txtSpecName.Text = ""; gdResult.Rows.Clear();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try { ExportToExcel(); } catch (Exception ex) { DisplayMessage(ex.Message, false); }
    }
    void SearchData(string index)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] = index.ToString();
        para["PageSize"] = m_PageSize.ToString();

        para["ProductName"] = txtProductName.Text.Trim();
        para["SpecName"] = txtSpecName.Text.Trim();

        para["GYY"] = txtGYY.Text;
        para["DEY"] = txtDEY.Text;
        para["StartDate"] = txtStartDate.Value;
        para["EndDate"] = txtEndDate.Value;

        uMESPagingDataDTO result = reportBal.GetSpecWorkHoursChangeInfo(para);

        this.gdResult.DataSource = result.DBTable;
        this.gdResult.DataBind();
        //给分页控件赋值，用于分页控件信息显示
        this.upageTurning.TotalRowCount = int.Parse(result.RowCount);
        this.upageTurning.RowCountByPage = m_PageSize;

    }

    DataTable SearchAllData()
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] = "all";
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["SpecName"] = txtSpecName.Text.Trim();

        para["GYY"] = txtGYY.Text;
        para["DEY"] = txtDEY.Text;

        para["StartDate"] = txtStartDate.Value;
        para["EndDate"] = txtEndDate.Value;

        uMESPagingDataDTO result = reportBal.GetSpecWorkHoursChangeInfo(para);

        return result.DBTable;

    }

    void ExportToExcel()
    {

        // DataTable dt = uMESCommonFunction.GetGridData(gdResult);

        DataTable dt = SearchAllData();
        //取有效列，且重命名列名
        DataView view = dt.DefaultView;

        dt = view.ToTable(false, "productname", "ProductDesc", "workflowname", "workflowrevision", "specname", "teamname", "factoryname", "unitworktime", "setupworktime",
            "tempunitworktime", "tempsetupworktime", "GYY", "CreateWorkflowDate", "DEY", "specworkhourschangeDate");

        dt.Columns["productname"].ColumnName = "图号";
        dt.Columns["ProductDesc"].ColumnName = "产品名称";
        dt.Columns["workflowname"].ColumnName = "工艺";
        dt.Columns["workflowrevision"].ColumnName = "工艺版本";
        dt.Columns["specname"].ColumnName = "工序";
        dt.Columns["teamname"].ColumnName = "班组";
        dt.Columns["factoryname"].ColumnName = "分厂";
        dt.Columns["tempunitworktime"].ColumnName = "单件工时";
        dt.Columns["tempsetupworktime"].ColumnName = "准备工时";
        dt.Columns["unitworktime"].ColumnName = "临时单件工时";
        dt.Columns["setupworktime"].ColumnName = "临时准备工时";
        dt.Columns["GYY"].ColumnName = "工艺员";
        dt.Columns["CreateWorkflowDate"].ColumnName = "工艺编制时间";
        dt.Columns["DEY"].ColumnName = "定额员";
        dt.Columns["specworkhourschangeDate"].ColumnName = "定额修改时间";
        
        string strPath = Request.MapPath(Request.ApplicationPath);
        string filePath = ExportHelper.DataTableExportToExcel2(dt, "临时工艺工时修改记录", strPath, new List<int>() { });
        string url = Request.Url.ToString();
        string baseUrl = Regex.Split(url, "/Report")[0];
        string fileUrl = baseUrl + filePath;


        string strScript = "<script>window.open('" + fileUrl + "');</script>";

        ExcuteScript(strScript);
    }


}